package com.zhuoxin.dao.admin;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.model.Course;
import com.zhuoxin.bean.model.Grade;
import com.zhuoxin.bean.model.Listen;
import com.zhuoxin.bean.model.ListenSO;
import com.zhuoxin.dao.BaseDAOImpl;

public class ListenDAOImpl extends BaseDAOImpl<Listen, ListenSO> implements ListenDAO {

	@Override
	public String getHqlWhere(ListenSO so) {
		return null;
	}

	@Override
	public void setCriteriaParams(Criteria criteria, ListenSO so) {
		if(so.getGrade()!=null){
			criteria.createAlias("grade", "g");
			
			if(so.getGrade().getId()!=null){
				criteria.add(Restrictions.eq("g.id", so.getGrade().getId()));
			}
		}
		
		if(so.getCourse()!=null){
			criteria.createAlias("course", "c");
			
			if(so.getCourse().getId()!=null){
				criteria.add(Restrictions.eq("c.id", so.getCourse().getId()));
			}
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			criteria.add(Restrictions.ilike("name", "%"+StringUtils.trim(so.getName())+"%"));
		}
		
		if(so.getFlag()!=null){
			criteria.add(Restrictions.eq("flag", true));
		}
		
		if(so.getStatus()!=null){
			criteria.add(Restrictions.eq("status", true));
		}
	}

	@Override
	public void setQueryParams(Query query, ListenSO so) {
		
	}

	@Override
	public void switchVO2PO(Listen po, ListenSO so) {
		if(so.getGrade()!=null && so.getGrade().getId()!=null){
			Grade grade=findById(Grade.class, so.getGrade().getId());
			po.setGrade(grade);
		}
		
		if(so.getCourse()!=null && so.getCourse().getId()!=null){
			Course course=findById(Course.class, so.getCourse().getId());
			po.setCourse(course);
		}
		
		if(so.getSex()!=null){
			po.setSex(so.getSex());
		}
		
		if(StringUtils.isNotEmpty(so.getName())){
			po.setName(StringUtils.trim(so.getName()));
		}
		
		if(StringUtils.isNotEmpty(so.getPhone())){
			po.setPhone(StringUtils.trim(so.getPhone()));
		}
		
		if(StringUtils.isNotEmpty(so.getCompany())){
			po.setCompany(StringUtils.trim(so.getCompany()));
		}
		
		if(StringUtils.isNotEmpty(so.getAddress())){
			po.setAddress(StringUtils.trim(so.getAddress()));
		}
		
		if(StringUtils.isNotEmpty(so.getQq())){
			po.setQq(StringUtils.trim(so.getQq()));
		}
		
		if(StringUtils.isNotEmpty(so.getEmail())){
			po.setEmail(StringUtils.trim(so.getEmail()));
		}
		
		if(StringUtils.isNotEmpty(so.getCause())){
			po.setCause(StringUtils.trim(so.getCause()));
		}
	}


}
